//
//  UIAlertTool.m
//  A_Over_Packaging
//
//  Created by ChenNing on 15/10/24.
//  Copyright © 2015年 chenn. All rights reserved.
//

#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] > 7.9)

#import "UIAlertTool.h"

typedef void (^confirm)();
typedef void (^cancle)();

@interface UIAlertTool()<UIActionSheetDelegate>
{
    confirm confirmParam;
    cancle  cancleParam;
}
@end

@implementation UIAlertTool

/**
 *  UIAlertView封装
 *
 *  @param viewController    调用的controller
 *  @param title             标题
 *  @param message           内容
 *  @param cancelButtonTitle 取消按钮
 *  @param otherButtonTitle  确定按钮
 *  @param confirm           取消回调方法
 *  @param cancle            确定回调方法
 */
- (void)showAlertView:(UIViewController *)viewController
                title:(NSString *)title
              message:(NSString *)message
    cancelButtonTitle:(NSString *)cancelButtonTitle
     otherButtonTitle:(NSString *)otherButtonTitle
              confirm:(void (^)())confirm
               cancle:(void (^)())cancle
{
    confirmParam=confirm;
    cancleParam=cancle;
    // iOS8.0以上
    NSLog(@"版本:%.2f",[[[UIDevice currentDevice] systemVersion] floatValue]);
    if (IAIOS8) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        if (cancelButtonTitle != nil) {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                cancle();
            }];
            [alertController addAction:cancelAction];
        }
        if (otherButtonTitle != nil) {
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                confirm();
            }];
            [alertController addAction:otherAction];
        }
        
        [viewController presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *TitleAlert =
        [[UIAlertView alloc] initWithTitle:title
                                   message:message
                                  delegate:self
                         cancelButtonTitle:otherButtonTitle
                         otherButtonTitles:cancelButtonTitle,nil];
        [TitleAlert show];
        CFRunLoopRun();
        // 确定按钮
        if (self.buttonIndex == 1) {
            confirm();
        }else{
            cancle();
        }
    }
}

- (void)showAlertSheet:(UIViewController *)viewController
                 title:(NSString *)title
        buttonTitleArr:(NSArray *)buttonTitleArr
           buttonIndex:(void (^)(NSInteger index))buttonIndex
{
    // iOS8.0以上
    if (IAIOS8) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        // Create the actions.
        for (NSInteger i = 0; i < buttonTitleArr.count; i++) {
            UIAlertAction *buttonAction = [UIAlertAction actionWithTitle:[buttonTitleArr objectAtIndex:i] style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                buttonIndex(i);
            }];
            [alertController addAction:buttonAction];
        }
        
        [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
        
        [viewController presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title
                                                           delegate:self
                                                  cancelButtonTitle:nil
                                             destructiveButtonTitle:nil
                                                  otherButtonTitles:nil];
        for (NSString *buttonTitle in buttonTitleArr) {
             [sheet addButtonWithTitle:buttonTitle];
        }
        sheet.cancelButtonIndex = sheet.numberOfButtons-1;
        [sheet showInView:viewController.view];
        CFRunLoopRun();
        buttonIndex(self.buttonIndex);
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.buttonIndex = buttonIndex;
//    actionSheet = nil;
    CFRunLoopStop(CFRunLoopGetCurrent());
}

@end
